/**
 * Copyright © 2014-2021 The SiteWhere Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sitewhere.grpc.client.common.kafka;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.protobuf.InvalidProtocolBufferException;
import com.sitewhere.grpc.kafka.model.KafkaModel.GMicroserviceLogMessage;
import com.sitewhere.grpc.kafka.model.KafkaModel.GStateUpdate;
import com.sitewhere.spi.SiteWhereException;

/**
 * Methods that support marshaling/unmarshaling Kafka payloads.
 */
public class KafkaModelMarshaler {

    /** Static logger instance */
    private static Logger LOGGER = LoggerFactory.getLogger(KafkaModelMarshaler.class);

    /**
     * Build binary message for GRPC state update.
     * 
     * @param grpc
     * @return
     * @throws SiteWhereException
     */
    public static byte[] buildStateUpdateMessage(GStateUpdate grpc) throws SiteWhereException {
	ByteArrayOutputStream output = new ByteArrayOutputStream();
	try {
	    grpc.writeTo(output);
	    return output.toByteArray();
	} catch (IOException e) {
	    throw new SiteWhereException("Unable to build state update message.", e);
	} finally {
	    closeQuietly(output);
	}
    }

    /**
     * Parse message that reflects a microservice state update.
     * 
     * @param payload
     * @return
     * @throws SiteWhereException
     */
    public static GStateUpdate parseStateUpdateMessage(byte[] payload) throws SiteWhereException {
	try {
	    return GStateUpdate.parseFrom(payload);
	} catch (InvalidProtocolBufferException e) {
	    throw new SiteWhereException("Unable to parse state update message.", e);
	}
    }

    /**
     * Build binary message for GRPC microservice log message.
     * 
     * @param grpc
     * @return
     * @throws SiteWhereException
     */
    public static byte[] buildMicroserviceLogMessage(GMicroserviceLogMessage grpc) throws SiteWhereException {
	ByteArrayOutputStream output = new ByteArrayOutputStream();
	try {
	    grpc.writeTo(output);
	    return output.toByteArray();
	} catch (IOException e) {
	    throw new SiteWhereException("Unable to build microservice log message.", e);
	} finally {
	    closeQuietly(output);
	}
    }

    /**
     * Parse binary content for microservice log message.
     * 
     * @param payload
     * @return
     * @throws SiteWhereException
     */
    public static GMicroserviceLogMessage parseMicroserviceLogMessage(byte[] payload) throws SiteWhereException {
	try {
	    return GMicroserviceLogMessage.parseFrom(payload);
	} catch (InvalidProtocolBufferException e) {
	    throw new SiteWhereException("Unable to parse microservice log message.", e);
	}
    }

    protected static void closeQuietly(OutputStream output) {
	if (output != null) {
	    try {
		output.close();
	    } catch (IOException e) {
		LOGGER.error("Unable to close output stream.", e);
	    }
	}
    }
}